package 最长回文子串;

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param A string字符串
     * @return int整型
     */
    static int N = 1010, n;
    static boolean[][] f = new boolean[N][N];
    static char[] arr = new char[N];

    public int getLongestPalindrome(String A) {
        arr = A.toCharArray();
        n = A.length();
        for (int i = 0; i < n; i++) {
            f[i][i] = true;
        }
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] == arr[i]) {
                f[i - 1][i] = true;
            }
        }
        int ret = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                if(dfs(i, j)){
                    ret = Math.max(ret, j - i + 1);
                }
            }
        }
        return ret;
    }

    private boolean dfs(int i, int j) {
        if (i < 0 || i >= n || j < 0 || j >= n) {
            return false;
        }
        if (f[i][j]) {
            return true;
        }
        if (arr[i] != arr[j]) {
            return false;
        }
        f[i][j] = dfs(i + 1, j - 1);
        return f[i][j];
    }
}